package com.chenwc.netty.tcp;

import com.chenwc.netty.tcp.config.NettyProperties;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NettyServerBoot {
	
	private static final Logger log = LoggerFactory.getLogger(NettyServerBoot.class);

	NioEventLoopGroup boosGroup;

	NioEventLoopGroup workerGroup;
    
	final ServerBootstrap serverBootstrap;

	final NettyProperties nettyProperties;

    public NettyServerBoot(ServerBootstrap serverBootstrap, NettyProperties nettyProperties) {
        this.serverBootstrap = serverBootstrap;
        this.nettyProperties = nettyProperties;
    }

    /**
     * 启动netty
     *
     */
    public void start() throws InterruptedException {
        // 绑定端口启动
        serverBootstrap.bind(nettyProperties.getPort()).sync();
        // 备用端口
        serverBootstrap.bind(nettyProperties.getPortSalve()).sync();
        log.info("启动 Netty: {},{}", nettyProperties.getPort(), nettyProperties.getPortSalve());
    }

    /**
     * 关闭netty
     */
    public void close() {
        log.info("关闭 Netty");
        boosGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
